/*
	enumimpl.h
	Created by kiminouso, 2022/11/29, Hohai University
	仿java枚举自动映射宏定义文件(实现用)
*/
#pragma once
#ifndef enumimpl_h__
#define enumimpl_h__

#include <string>

#undef ENUM_BEGIN
#undef ENUM_ENTRY
#undef ENUM_END

#define ENUM_BEGIN(classname) \
std::map<u_int64_t, string_ptr > classname::str(\

#define ENUM_ENTRY(txt, id, val) {id,#val}

#define ENUM_END(classname) );\
classname::classname(){\
	for (auto it = str.begin(); it != str.end(); ++it) {\
		str_int[it->second] = it->first;\
	}\
}\
classname::_MyEnum& classname::operator=(classname::_MyEnum& val) {\
		_enum = val; \
		currstr = str.find((int)val)->second; \
		return _enum; \
}\
classname::_MyEnum& classname::operator=(classname& val){\
		_enum=val._enum;\
		currstr=str.find((int)val._enum)->second;\
		return _enum;\
}\
bool classname::operator==(classname& rgt) const{\
	return _enum==rgt._enum;\
}\
bool classname::operator==(_MyEnum& rgt) const{\
	return _enum==rgt;\
}\
int classname::value() const{\
	return (int)(_enum);\
}\
string_ptr  classname::toString() const{\
		return currstr;\
}\
string_ptr  classname::toString(classname::_MyEnum& val){\
		if(str.find((int)val)!=str.end())\
			return str.find((int)val)->second;\
		else return "";\
}\
u_int64_t classname::of(string_ptr  type){\
	if (type.empty()) {\
		return 0;\
	}\
	u_int64_t v = 0;\
	size_t flagSub = 0;\
	for (size_t i = 0; i < type.length(); i++) {\
		if (type[i] == ',') {\
			string_ptr  temp = type.substr(flagSub, i - flagSub);\
			if (str_int.find(temp) != str_int.end())\
				v |= str_int.find(temp)->second;\
			flagSub = i + 1;\
		}\
	}\
	if (type.length() > flagSub) {\
		string_ptr  temp = type.substr(flagSub, type.length() - flagSub);\
		if (str_int.find(temp) != str_int.end())\
			v |= str_int.find(temp)->second;\
	}\
	return v;\
}
#endif // enumimpl_h__

